package com.zycfc.zsf.boot.util.collection;

import com.google.common.collect.*;
import java.util.*;
import org.apache.commons.lang3.tuple.*;

public abstract class CollectionUtils
{
    public static boolean isEmpty(final Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }
    
    public static boolean isNotEmpty(final Collection<?> collection) {
        return collection != null && !collection.isEmpty();
    }
    
    public static <T> T getFirst(final Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }
        if (collection instanceof List) {
            return ((List)collection).get(0);
        }
        return collection.iterator().next();
    }
    
    public static <T> T getLast(final Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }
        if (collection instanceof List) {
            final List<T> list = (List<T>)(List)collection;
            return list.get(list.size() - 1);
        }
        return (T)Iterators.getLast((Iterator)collection.iterator());
    }
    
    public static boolean elementsEqual(final Iterable<?> iterable1, final Iterable<?> iterable2) {
        return Iterables.elementsEqual((Iterable)iterable1, (Iterable)iterable2);
    }
    
    public static <T extends Object> T min(final Collection<? extends T> coll) {
        return Collections.min(coll);
    }
    
    public static <T> T min(final Collection<? extends T> coll, final Comparator<? super T> comp) {
        return Collections.min(coll, comp);
    }
    
    public static <T extends Object> T max(final Collection<? extends T> coll) {
        return Collections.max(coll);
    }
    
    public static <T> T max(final Collection<? extends T> coll, final Comparator<? super T> comp) {
        return Collections.max(coll, comp);
    }
    
    public static <T extends Object> Pair<T, T> minAndMax(final Collection<? extends T> coll) {
        final Iterator<? extends T> i = coll.iterator();
        T maxCandidate;
        T minCandidate = maxCandidate = (T)i.next();
        while (i.hasNext()) {
            final T next = (T)i.next();
            if (((Comparable)next).compareTo(minCandidate) < 0) {
                minCandidate = next;
            }
            else {
                if (((Comparable)next).compareTo(maxCandidate) <= 0) {
                    continue;
                }
                maxCandidate = next;
            }
        }
        return (Pair<T, T>)new ImmutablePair((Object)minCandidate, (Object)maxCandidate);
    }
    
    public static <T> Pair<T, T> minAndMax(final Collection<? extends T> coll, final Comparator<? super T> comp) {
        final Iterator<? extends T> i = coll.iterator();
        T maxCandidate;
        T minCandidate = maxCandidate = (T)i.next();
        while (i.hasNext()) {
            final T next = (T)i.next();
            if (comp.compare((Object)next, (Object)minCandidate) < 0) {
                minCandidate = next;
            }
            else {
                if (comp.compare((Object)next, (Object)maxCandidate) <= 0) {
                    continue;
                }
                maxCandidate = next;
            }
        }
        return (Pair<T, T>)new ImmutablePair((Object)minCandidate, (Object)maxCandidate);
    }
}
